import mesh.Cube;
import mesh.Mesh;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
 
public class Perma
{
	
	private float n = 0.0f;
	
	private Mesh mesh;
	private Cube cube;
	
	private int fps; 
	private long lastFPS;
	
    public void start()
    {
    	try
    	{
		    Display.setDisplayMode(new DisplayMode(800, 600));
		    Display.create();
    	}
    	catch (LWJGLException e)
    	{
		    e.printStackTrace();
		    System.exit(0);
		}
    	
    	loadModels();
		lastFPS = getTime();
		
		while (!Display.isCloseRequested())
		{
			render();
			
			
		    
		    n+= 0.02;
		}
	 
		Display.destroy();
    }
    
    private void loadModels()
	{
    	mesh = new Mesh();
    	mesh.loadFile("data/cube.obj");
    	
    	cube = new Cube();
		// TODO Auto-generated method stub
		
	}

	private void render()
    {
		
    	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    	GL11.glLoadIdentity();
    	GL11.glRotatef(45, 0, 1, 0);
    	GL11.glRotatef(45, 1, 0, 0);
    	GL11.glRotatef(n, 1, 0, 0);
    	mesh.render();
		updateFPS();
			
//			GL11.glBegin(GL11.GL_TRIANGLES);
//		    GL11.glColor3f(0.5f,0.5f,1.0f);
//			GL11.glVertex3f( 0.0f, 0.5f, -0.5f);
//		    GL11.glColor3f(0.5f,1f,1.0f);
//			GL11.glVertex3f(-0.5f,-0.5f, -0.5f);
//		    GL11.glColor3f(1.0f,1f,1.0f);
//			GL11.glVertex3f( 0.5f,-0.5f, -0.5f);
//		    GL11.glEnd();
//	 
		    Display.update();
    	
    }
	public void updateFPS()
	{
	    if (getTime() - lastFPS > 1000)
	    {
	        Display.setTitle("FPS: " + fps); 
	        fps = 0; //reset the FPS counter
	        lastFPS += 1000; //add one second
	    }
	    fps++;
	}
	
	public long getTime()
	{
		return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}

	public static void main(String[] argv) {
        Perma quadExample = new Perma();
        quadExample.start();
    }
}